package config

import (
	"github.com/spf13/viper"
)

var GlobalConfig Config

type Config struct {
	Server struct {
		Host string `mapstructure:"host"`
		Port int    `mapstructure:"port"`
		Mode string `mapstructure:"mode"`
	} `mapstructure:"server"`

	Database struct {
		Driver    string `mapstructure:"driver"`
		Host      string `mapstructure:"host"`
		Port      int    `mapstructure:"port"`
		Username  string `mapstructure:"username"`
		Password  string `mapstructure:"password"`
		DBName    string `mapstructure:"dbname"`
		Charset   string `mapstructure:"charset"`
		ParseTime bool   `mapstructure:"parseTime"`
		Loc       string `mapstructure:"loc"`
	} `mapstructure:"database"`

	ZeroMQ struct {
		LogReceiverAddress string `mapstructure:"log_receiver_address"`
	} `mapstructure:"zeromq"`

	Log struct {
		Level  string `mapstructure:"level"`
		Format string `mapstructure:"format"`
		Output string `mapstructure:"output"`
	} `mapstructure:"log"`
}

func Init() error {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath("config")

	if err := viper.ReadInConfig(); err != nil {
		return err
	}

	if err := viper.Unmarshal(&GlobalConfig); err != nil {
		return err
	}

	return nil
}
